package cn.edu.ljl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class updateExperience extends HttpServlet {
    private static final String UPDATE_TEMPLATE =
            "update courses set nam ='%s',course='%s',advice='%s' where id = " ;

    private static final String SELECT =
            "SELECT * FROM courses WHERE id=";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String sql01=SELECT+EditExperience.Id;
        String name   = null;
        String course = null;

        try {
            List<Experience> experiences = DBMS.getExperience(sql01);
            Experience experience = experiences.get(0);
            name = experience.getName();
            course = experience.getCourse();
        } catch (SQLException e) {
            e.printStackTrace();
        }


        String advice = request.getParameter("advice");


        String id1 = EditExperience.Id;

        String sql = String.format(UPDATE_TEMPLATE,name,course,advice);
        String sql1 = sql + id1;
        System.out.println(sql1);

        try {
            DBMS.update(sql1);
            response.sendRedirect("./listExperience");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
